﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzyGoEzySystem.Services;
using EzyGoEzySystem.Models;
using EzyGoEzySystem.Services.DataStructures;

namespace EzyGoEzySystem.Controllers
{
    public class RouteSuggestController : Controller
    {
        //
        // GET: /RouteSuggest/

        
        public ActionResult Index()
        {

            return RedirectToAction("Find");
        }


        public ActionResult Find()
        {
            RouteSuggestModel model = new RouteSuggestModel();
            model.Sources = new SelectList(GetSourceStop(), "Source", "Source");
            model.Destinations = new SelectList(GetDestinationStop(), "Destination", "Destination");
            return View(model);

        }

        [HttpPost]
        public ActionResult Find(RouteSuggestModel model, string returnUrl)
        {
            using (var dbContext = new EzyGoEzyDBContext())
            {
                TrafficTracking bestRoute = new TrafficTracking(dbContext);
                List<Node> routes = bestRoute.SuggestRoute(model.Source, model.Destination);
                string result = "";
                for (int i = routes.Count - 1; i >= 1; i--)
                {
                    List<string> buses = bestRoute.bus(routes.ElementAt(i).BusStopName, routes.ElementAt(i - 1).BusStopName, dbContext);
                    result += routes.ElementAt(i).BusStopName + " -> " + routes.ElementAt(i - 1).BusStopName + " by Bus ";
                    foreach (var bus in buses)
                    {
                        result += bus + " ";
                    }
                    result += ";";
                }
                //result += routes.ElementAt(0).BusStopName;
                model = new RouteSuggestModel();
                model.Sources = new SelectList(GetSourceStop(), "Source", "Source");
                model.Destinations = new SelectList(GetDestinationStop(), "Destination", "Destination");
                model.Route = result;

                return View(model);
            }

        }

        public class SourceStop
        {
            public string Source { get; set; }
            public SourceStop(string name)
            {
                this.Source = name;
            }
        }

        public class DestinationStop
        {
            public string Destination { get; set; }
            public DestinationStop(string destination)
            {
                this.Destination = destination;
            }
        }

        public List<SourceStop> GetSourceStop()
        {
            List<BusStop> data = DataAccess.GetBusStop();
            List<SourceStop> displays = new List<SourceStop>();
            foreach(var busstop in data)
            {
                displays.Add(new SourceStop(busstop.Name));
            }
            return displays;
        }

        public List<DestinationStop> GetDestinationStop()
        {
            List<BusStop> data = DataAccess.GetBusStop();
            List<DestinationStop> displays = new List<DestinationStop>();
            foreach (var busstop in data)
            {
                displays.Add(new DestinationStop(busstop.Name));
            }
            return displays;
        }

        //
        // GET: /RouteSuggest/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /RouteSuggest/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /RouteSuggest/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /RouteSuggest/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /RouteSuggest/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // GET: /RouteSuggest/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }
        //
        // POST: /RouteSuggest/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
